package lunchy.data.classes;

import java.util.ArrayList;

import lunchy.data.interfaces.ILunchyMenu;
import lunchy.data.interfaces.ILunchyMenuGroup;
import lunchy.data.interfaces.ILunchyMenuItem;
import lunchy.main.CLunchyApplication;

public class CLunchyMenu implements ILunchyMenu
{
	private ArrayList<ILunchyMenuGroup> groups = new ArrayList<ILunchyMenuGroup>(0);

	private Integer maxGroupId = 0;
	private Integer maxItemId = 0;
	
	public boolean addMenuGroup(ILunchyMenuGroup group)
	{
		if(group != null)
		{
			groups.add(group);
			return true;
		}
		else
		{
			return false;
		}
	}

	public ILunchyMenuGroup getMenuGroup(int index)
	{
		if(index > -1 && index < groups.size())
			return groups.get(index);
		else
			return null;
	}
	
	public ArrayList<ILunchyMenuGroup> getMenuGroups()
	{
		return groups;
	}

	public ILunchyMenuItem findMenuItemById(Integer id)
	{
		for (ILunchyMenuGroup group : groups)
		{
			for (ILunchyMenuItem item : group.getItems())
			{
				if(item.getId().equals(id))
					return item;
			}
		}

		return null;
	}
	
	public ILunchyMenuGroup findMenuGroupById(Integer id)
	{
		for (ILunchyMenuGroup group : groups)
		{
			if(group.getId().equals(id))
				return group;
		}

		return null;
	}

	public Integer getMaxGroupId()
	{
		return maxGroupId;
	}

	public Integer getMaxItemId()
	{
		return maxItemId;
	}

	public Integer increaseMaxGroupId()
	{
		return ++maxGroupId;
	}

	public Integer increaseMaxItemId()
	{
		return ++maxItemId;
	}

	public void setMaxGroupId(Integer maxId)
	{
		if(maxId > maxGroupId)
			maxGroupId = maxId;
	}

	public void setMaxItemId(Integer maxId)
	{
		if(maxId > maxItemId)
			maxItemId = maxId;
	}

	public void deleteGroup(Integer groupId)
	{
		groups.remove(findMenuGroupById(groupId));
	}

	public void deleteItem(Integer itemId)
	{
		ILunchyMenuItem delItem = findMenuItemById(itemId);

		for (ILunchyMenuGroup group : groups)
		{
			group.getItems().remove(delItem);
		}
		
	}
}
